package com.maxima.attendance.actions;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.maxima.attendance.daos.HolidayDAO;
import com.maxima.attendance.daos.ProjectDAO;
import com.maxima.attendance.daos.TimeLogDAO;
import com.maxima.attendance.daos.UserDAO;
import com.maxima.attendance.pojos.Employee;
import com.maxima.attendance.pojos.Holiday;
import com.maxima.attendance.pojos.Project;
import com.maxima.attendance.pojos.User;
import com.opensymphony.xwork2.ActionSupport;

public class InitializeList extends ActionSupport implements SessionAware {
	private static final long serialVersionUID = 1L;
	private TimeLogDAO timeLogDAO = new TimeLogDAO();
	private Map<String, Object> map;
	public static List<Employee> employeeList;
	public static List<Project> projectList;
	public static List<Holiday> holidayList;

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}

	public String initCurrentEmployees() {
		if (SessionManager.validateAdminSession()) {
			this.setEmployeeList(initEmployeeList());
			map.put(SessionManager.CURRENT_EMPLOYEE_LIST, employeeList);
			return "admin";
		} else if (SessionManager.validateUserSession()) {
			this.setEmployeeList(initEmployeeList());
			map.put(SessionManager.CURRENT_EMPLOYEE_LIST, employeeList);
			return "user";
		} else {
			return ERROR;
		}
	}

	public String initAllEmployees() {
		if (SessionManager.validateAdminSession()) {
			this.setEmployeeList(initAllEmployeeList());
			map.put(SessionManager.ALL_EMPLOYEE_LIST, employeeList);
			InitializeList.setProjectList(initProjectList());
			map.put(SessionManager.SELECTED_PROJECT, null);
			map.put(SessionManager.PROJECT_LIST, projectList);
			return SUCCESS;
		} else {
			return ERROR;
		}
	}

	public String initProjects() {
		if (SessionManager.validateAdminSession()) {
			InitializeList.setProjectList(initProjectList());
			map.put(SessionManager.SELECTED_PROJECT, null);
			map.put(SessionManager.PROJECT_LIST, projectList);
			return SUCCESS;
		} else {
			return ERROR;
		}
	}

	public String initHolidays() {
		if (SessionManager.validateAdminSession()) {
			InitializeList.setHolidayList(initHolidayList());
			map.put(SessionManager.SELECTED_HOLIDAY, null);
			map.put(SessionManager.HOLIDAY_LIST, holidayList);
			return SUCCESS;
		} else {
			return ERROR;
		}
	}

	public String initWorkFromHomeEmployees() {
		if (SessionManager.validateAdminSession()) {
			String str = ServletActionContext.getRequest().getParameter("date");
			Date date = null;
			if (str != null) {
				try {
					date = new SimpleDateFormat("dd/MM/yyyy").parse(str);
				}
				catch (ParseException e) {
					e.printStackTrace();
				}
			}
			map.put(SessionManager.WORK_FROM_HOME_EMPLOYEE_LIST, initWorkFromHomeEmployeeList(date));
			map.put(SessionManager.SELECTED_DATE, str);
			return SUCCESS;
		} else {
			return ERROR;
		}
	}

	private List<Employee> initEmployeeList() {
		Date date = new Date();
		List<User> usersList = new UserDAO().getAllActiveUsers();
		List<Employee> employeeList = new ArrayList<Employee>();
		for (User user : usersList) {
			employeeList.add(new Employee(user, timeLogDAO.getTimeLogByDate(user.getId(), date)));
		}
		return employeeList;
	}

	private List<Employee> initAllEmployeeList() {
		Date date = new Date();
		List<User> usersList = new UserDAO().getAllUsers();
		List<Employee> employeeList = new ArrayList<Employee>();
		for (User user : usersList) {
			employeeList.add(new Employee(user, timeLogDAO.getTimeLogByDate(user.getId(), date)));
		}
		return employeeList;
	}

	private List<Project> initProjectList() {
		List<Project> proList = new ProjectDAO().getAll();
		List<Project> projectList = new ArrayList<Project>();
		for (Project project : proList) {
			projectList.add(new Project(project));
		}
		return projectList;
	}

	private List<Holiday> initHolidayList() {
		List<Holiday> holiList = new HolidayDAO().getAllHolidays();
		List<Holiday> holidayList = new ArrayList<Holiday>();
		for (Holiday holiday : holiList) {
			holidayList.add(new Holiday(holiday));
		}
		return holidayList;
	}

	private List<Employee> initWorkFromHomeEmployeeList(Date date) {
		List<User> usersList = new UserDAO().getAllActiveUsers();
		List<Employee> employeeList = new ArrayList<Employee>();
		for (User user : usersList) {
			employeeList.add(new Employee(user, timeLogDAO.getTimeLogByDate(user.getId(), date)));
		}
		return employeeList;
	}

	@Override
	public void setSession(Map<String, Object> m) {
		this.map = m;
	}

	public Map<String, Object> getMap() {
		return map;
	}

	public void setMap(Map<String, Object> map) {
		this.map = map;
	}

	public static List<Project> getProjectList() {
		return projectList;
	}

	public static void setProjectList(List<Project> projectList) {
		InitializeList.projectList = projectList;
	}

	public List<Employee> getEmployeeList() {
		return employeeList;
	}

	public void setEmployeeList(List<Employee> employeeList) {
		InitializeList.employeeList = employeeList;
	}

	public static List<Holiday> getHolidayList() {
		return holidayList;
	}

	public static void setHolidayList(List<Holiday> holidayList) {
		InitializeList.holidayList = holidayList;
	}
}
